package cn.com.utt.unms.controller;

import java.security.Principal;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import cn.com.utt.common.service.RoleService;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.Role;
import cn.com.utt.common.vo.User;

@RestController
@RequestMapping("v1/roles")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@RequestMapping(value = "/all", method = { RequestMethod.GET })
	public Map<String, Object> roles(Principal principal,Long childrenId,Long type) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		List<Role> roles = roleService.queryRoleList(user,childrenId,type);
		result.put("roles", roles);
		return result.toJsonObject();
	}
	
	@RequestMapping(value = "/menu", method = { RequestMethod.GET })
	public Map<String, Object> menu(HttpServletRequest request, HttpServletResponse response, Principal principal) {
		String resourceTableName = "resource";
		String roleResourceTableName = "role_resource";
		String serverName = request.getServerName();
		if (serverName.equals("cloud.anphat.vn")){
			resourceTableName = "vn_resource";
			roleResourceTableName = "vn_role_resource";
		}
		Authentication authentication = (Authentication) principal;
		User user = (User)authentication.getPrincipal();
		return roleService.queryMenu(user.getId(), resourceTableName, roleResourceTableName);
	}
	
}